package de.tud.etit.blipz.services.custom;

import java.util.ArrayList;

import de.tud.etit.blipz.entities.Contact;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;

public class AndroidService {
	private static final String[] PHOTO_ID_PROJECTION = new String[] { PhoneLookup.PHOTO_ID };

	private static final String[] PHOTO_BITMAP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO };

	private static final String[] DISPLAYNAME_PROJECTION = new String[] { PhoneLookup.DISPLAY_NAME };

	public static String resolveNameOfContact(ContentResolver resolver,
			String mobileNr) {
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(mobileNr));
		Cursor cursor = resolver.query(uri, DISPLAYNAME_PROJECTION, null, null,
				null);

		String name = null;

		if (cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				name = cursor
						.getString(cursor
								.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			}
		}

		return name;
	}

	public static Bitmap fetchContactPhoto(ContentResolver resolver,
			String mobileNr) {
		final Integer contactPhotoId = fetchContactPhotoId(resolver, mobileNr);

		Bitmap contactPhoto = null;

		if (contactPhotoId != null) {
			contactPhoto = fetchContactPhoto(resolver, contactPhotoId);
		}

		return contactPhoto;
	}

	public static ArrayList<Contact> getAllPhoneContacts(Context context) {
		ArrayList<Contact> allContacts = new ArrayList<Contact>();

		Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
		Cursor cursor = context.getContentResolver().query(
				uri,
				new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
						ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
						ContactsContract.CommonDataKinds.Phone._ID }, null,
				null,
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

		try {
			cursor.moveToFirst();
			while (cursor.isAfterLast() == false) {
				String contactNumber = cursor
						.getString(cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				String contactName = cursor
						.getString(cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

				Contact contact = new Contact();
				contact.setMobileNr(contactNumber);
				contact.setName(contactName);

				if (contact != null && contact.getMobileNr() != null
						&& contact.getMobileNr().isEmpty() == false) {
					allContacts.add(contact);
				}

				contact = null;
				cursor.moveToNext();
			}
		} catch (Exception ex) {
		} finally {
			cursor.close();
			cursor = null;
		}

		return allContacts;
	}

	private static Integer fetchContactPhotoId(ContentResolver resolver,
			String mobileNr) {

		final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(mobileNr));
		final Cursor cursor = resolver.query(uri, PHOTO_ID_PROJECTION, null,
				null, null);

		try {
			Integer contactPhotoId = null;
			if (cursor.moveToFirst()) {
				contactPhotoId = cursor.getInt(cursor
						.getColumnIndex(PhoneLookup.PHOTO_ID));
			}
			return contactPhotoId;
		} finally {
			cursor.close();
		}

	}

	private final static Bitmap fetchContactPhoto(ContentResolver resolver,
			final int contactPhotoId) {

		final Uri uri = ContentUris.withAppendedId(
				ContactsContract.Data.CONTENT_URI, contactPhotoId);
		final Cursor cursor = resolver.query(uri, PHOTO_BITMAP_PROJECTION,
				null, null, null);

		try {
			Bitmap contactPhoto = null;
			if (cursor.moveToFirst()) {
				final byte[] contactPhotoBytes = cursor.getBlob(0);
				if (contactPhotoBytes != null) {
					contactPhoto = BitmapFactory.decodeByteArray(
							contactPhotoBytes, 0, contactPhotoBytes.length);
				}
			}
			return contactPhoto;
		} finally {
			cursor.close();
		}
	}
}